package com.darksider.darkotlin.utils

import io.reactivex.Flowable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import java.util.concurrent.TimeUnit

/**
 * User: yaodong(1126220529@qq.com)
 * Date: 2017-05-26
 * Time: 16:11
 * Description: RxJava 帮助类
 * Update:
 */
class RxHelper {

    companion object{
        /**
         * 倒计时
         * @param time
         * *
         * @return
         */
        fun countdown(time: Int): Flowable<Int> {
            var time = time
            if (time < 0) {
                time = 0
            }
            val countTime = time

            return Flowable.interval(0, 1, TimeUnit.SECONDS)
                    .map({ increaseTime -> countTime - increaseTime!!.toInt() })
                    .take(countTime.toLong() + 1)
                    .subscribeOn(Schedulers.io())
                    .unsubscribeOn(Schedulers.io())
                    .subscribeOn(AndroidSchedulers.mainThread())
                    .observeOn(AndroidSchedulers.mainThread())

        }
    }

}